//
//  YYNewFeatureViewController.swift
//  YYWeibo
//
//  Created by strongwallyi on 16/1/1.
//  Copyright © 2016年 strongwallyi. All rights reserved.
//

import UIKit

class YYNewFeatureViewController: UIViewController, UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI(){
        //添加视图及约束
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        
        scrollView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_bottom).offset(-100)
        }
        
        //for 循环添加图片
        let count = 4
        
        for i in 0..<count {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            
            imageView.frame = CGRect(x: CGFloat(i) * SCREENW, y: 0, width: SCREENW, height: SCREENH)
            scrollView.addSubview(imageView)
            
            if i == count - 1{
                setupLastPage(imageView)
            }
        }
        
        //设置内容大小,可以滚动
        scrollView.contentSize = CGSizeMake(CGFloat(count) * SCREENW, 0)
        //设置 pageControl 的页数
        pageControl.numberOfPages = count
    }
    
    //设置最后一页的内容
    private func setupLastPage(imageView: UIImageView){
        
        imageView.userInteractionEnabled = true
        imageView.addSubview(enterBtn)
        imageView.addSubview(sharedBtn)
        
        //添加约束
        enterBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imageView)
            make.centerY.equalTo(imageView.snp_bottom).offset(-150)
        }
        
        sharedBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(enterBtn)
            make.bottom.equalTo(enterBtn.snp_top).offset(-20)
        }
    }
    
    //设置两个按钮的点击方法
    @objc private func enterBtnClick(){
//        print("切换控制器")
        NSNotificationCenter.defaultCenter().postNotificationName(YYSwitchRootViewControllerNotification, object: nil)
    }
    
    @objc private func sharedBtnClick(button: UIButton){
        button.selected = !button.selected
    }
    
    //scrollView 的代理方法,用来让 page 和图片想对应
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.frame.width + 0.5
        pageControl.currentPage = Int(page)
    }

    //懒加载控件--scrollView
    private lazy var scrollView:UIScrollView = {
    
        let scrollView = UIScrollView()
        
//        scrollView.backgroundColor = UIColor.purpleColor()
        //开启分页
        scrollView.pagingEnabled = true
        //关闭弹簧
        scrollView.bounces = false
        //关闭滚动条
        scrollView.showsHorizontalScrollIndicator = false
        //设置代理
        scrollView.delegate = self
        return scrollView
    }()
    
    //懒加载控件--pageControl
    private lazy var pageControl:UIPageControl = {
        let pageControl = UIPageControl()
        //设置不同状态的颜色
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        return pageControl
    }()
    
    //懒加载进入微博的 btn
    private lazy var enterBtn:UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self, action: "enterBtnClick", forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        btn.setTitle("进入微博", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return btn
    }()
    
    //懒加载分享 btn
    private lazy var sharedBtn:UIButton = {
        let btn = UIButton()
        //设置点击方法
        btn.addTarget(self, action: "sharedBtnClick:", forControlEvents: .TouchUpInside)
        
        btn.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        btn.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        
        //设置文字以及文字属性
        btn.setTitle("分享到微博", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        //调整边距
        btn.titleEdgeInsets = UIEdgeInsetsMake( 0, 5, 0 ,-5)
        
        return btn
    }()
    

}
